<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use app\portal\model\PortalCategoryModel;
use app\portal\service\PostService;
use app\portal\model\PortalPostModel;

class ArticleController extends HomeBaseController
{
    /**
     * 文章详情
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {

        $portalCategoryModel = new PortalCategoryModel();
        $postService         = new PostService();

        $articleId  = $this->request->param('id', 0, 'intval');
        $categoryId = $this->request->param('cid', 0, 'intval');
        $article    = $postService->publishedArticle($articleId, $categoryId);

        if (empty($article)) {
            abort(404, '文章不存在!');
        }


        $prevArticle = $postService->publishedPrevArticle($articleId, $categoryId);
        $nextArticle = $postService->publishedNextArticle($articleId, $categoryId);

        $tplName = 'article';

        if (empty($categoryId)) {
            $categories = $article['categories'];

            if (count($categories) > 0) {
                $this->assign('category', $categories[0]);
            } else {
                abort(404, '文章未指定分类!');
            }

        } else {
            $category = $portalCategoryModel->where('id', $categoryId)->where('status', 1)->find();

            if (empty($category)) {
                abort(404, '文章不存在!');
            }

            $this->assign('category', $category);

            $tplName = empty($category["one_tpl"]) ? $tplName : $category["one_tpl"];
        }

        PortalPostModel::where('id', $articleId)->inc('post_hits')->update();


        hook('portal_before_assign_article', $article);

        $this->assign('article', $article);
        $this->assign('prev_article', $prevArticle);
        $this->assign('next_article', $nextArticle);

        $tplName = empty($article['more']['template']) ? $tplName : $article['more']['template'];

        return $this->fetch("/$tplName");
    }

    // 文章点赞
    public function doLike()
    {
        $this->checkUserLogin();
        $articleId = $this->request->param('id', 0, 'intval');


        $canLike = cmf_check_user_action("posts$articleId", 1);

        if ($canLike) {
            PortalPostModel::where('id', $articleId)->inc('post_like')->update();

            $this->success("赞好啦！");
        } else {
            $this->error("您已赞过啦！");
        }
    }

    /***
     * 会员中心文章列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function myindex()
    {
        if (!cmf_is_user_login())
        {
            return redirect($this->request->root() . "/");
        }
        $user = cmf_get_current_user();
        $id                  = $this->request->param('id', 0, 'intval');
        $gdstatus                  = $this->request->param('gdstatus');
        $keyword = $this->request->param('keyword');
        $portalCategoryModel = new PortalCategoryModel();

        $category = $portalCategoryModel->where('id', $id)->where('status', 1)->find();
       
        $this->assign('category', $category);
        $this->assign('gdstatus', $gdstatus);
        $this -> assign("keyword", $keyword);
        return $this->fetch();
    }

     /***
     * 会员中心文章列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function orderlist()
    {
        if (!cmf_is_user_login())
        {
            return redirect($this->request->root() . "/");
        }
        $user = cmf_get_current_user();
        $id                  = $this->request->param('id', 0, 'intval');
        $gdstatus                  = $this->request->param('gdstatus');
        $keyword = $this->request->param('keyword');
        $portalCategoryModel = new PortalCategoryModel();

        $category = $portalCategoryModel->where('id', $id)->where('status', 1)->find();
       
        $this->assign('category', $category);
        $this->assign('gdstatus', $gdstatus);
        $this -> assign("keyword", $keyword);
        
        return $this->fetch();
    }

    /**
     * 工单详情
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        $this->checkUserLogin();
        $portalCategoryModel = new PortalCategoryModel();
        $postService         = new PostService();

        $articleId  = $this->request->param('id', 0, 'intval');
        $categoryId = $this->request->param('cid', 0, 'intval');
        $article    = $postService->publishedArticlegd($articleId, $categoryId);

        if (empty($article)) {
            abort(404, '文章不存在!');
        }

        $tplName = 'article';

        if (empty($categoryId)) {
            $categories = $article['categories'];

            if (count($categories) > 0) {
                $this->assign('category', $categories[0]);
            } else {
                abort(404, '文章未指定分类!');
            }

        } else {
            $category = $portalCategoryModel->where('id', $categoryId)->where('status', 1)->find();

            if (empty($category)) {
                abort(404, '文章不存在!');
            }

            $this->assign('category', $category);

            $tplName = empty($category["one_tpl"]) ? $tplName : $category["one_tpl"];
        }

        PortalPostModel::where('id', $articleId)->inc('post_hits')->update();


        hook('portal_before_assign_article', $article);
        $article['fwymore'] = json_decode($article['fwymore'],true);
        $fwimg = $article['fwymore']['fwimg'];
        //$this->error($fwimg);
        $article['fwimg'] = explode(";",$fwimg);
        $this->assign('article', $article);

        $tplName = empty($article['more']['template']) ? $tplName : $article['more']['template'];

        return $this->fetch();
    }

    // 文章点赞
    public function changestatus()
    {
        $this->checkUserLogin();
        $articleId = $this->request->param('id', 0, 'intval');
        $gdstatus = $this->request->param('gdstatus');

        if($gdstatus == ''){
            $this->error("工单状态错误");
        }

        
        PortalPostModel::where('id', $articleId)->update(['gdstatus' => $gdstatus]);

        $this->success("成功");
       
    }

    // 服务完成，提交
    public function uploadPost()
    {
        $file = $this->request->file('file');
    }
    public function gdeditPost()
    {
        
       
        $this->checkUserLogin();
        $data = $this->request->param();
        if($data['article']['fwbeizhu'] == ''){
            $this->error("请填写备注");
        }
        if($data['article']['fwimg'] == ''){
            $this->error('请上传图片');
        }
       
        $fwymore = [
                                        'fwbeizhu' => $data['article']['fwbeizhu'],
                                        'fwimg' => $data['article']['fwimg']
            ];
        
        PortalPostModel::where('id', $data['article']['id'])->json(['fwymore'])->update(['fwymore' => $fwymore,'gdstatus' => '待审核']);

        $this->success("成功", url('user/Profile/center'));
       
    }

    public function uploads()
    {
        
        $file = $this->request->file('image');
        //$this->error($file);
        $savename = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);
        //return($file);
        $this->error($savename);
       
    }

}
